﻿using ProjectGamer.Data;
using ProjectGamer.DataModels;
using ProjectGamer.Web.DbAdapters.IAdapters;
using ProjectGamer.Web.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.ServiceModel;

namespace ProjectGamer.Web.DbAdapters.Adapters
{
    public class GameAdapter : IGameAdapter
    {
        public List<Models.GameDetailVM> GetSearchedGames(string s)
        {


            List<Models.GameDetailVM> games;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                games = db.Games.GroupBy(x => x.Title).Select(y => y.FirstOrDefault()).Where(x => x.IsSold == false && x.IsRented == false && x.Title.Contains(s)).OrderBy(x => x.Title).Select(y => new GameDetailVM()
                {
                    Id = y.Id,
                    ApiKey = y.ApiKey,
                    UserId = "",
                    UserName = "",
                    Title = y.Title,
                    Genre = y.Genre,
                    Platform = y.Platform,
                    SellPrice = 0,
                    RentPrice = 0,
                    WillTrade = y.WillTrade,
                    Condition = y.Condition,
                }).ToList();
            }
            return games;

        }
        public string GetUserId(int id)
        {
            Game game;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                game = db.Games.Find(id);
            }
            return game.ApplicationUserId;
        }
        public Game AddGame(Game game)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                game = db.Games.Add(game);
                db.SaveChanges();
            }
            return game;
        }
        public List<Models.GameDetailVM> GetAllGames()
        {
            List<Models.GameDetailVM> games;
            List<Game> gamelist;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                gamelist = db.Games.OrderBy(x => x.Title).Where(x => x.IsRented == false && x.IsSold == false).ToList();
                games = gamelist.GroupBy(x => x.Title).Select(y => y.FirstOrDefault()).Select(y => new GameDetailVM()
                                {
                                    Id = y.Id,
                                    ApiKey = y.ApiKey,
                                    UserId = "",
                                    UserName = "",
                                    Title = y.Title,
                                    Genre = y.Genre,
                                    Platform = y.Platform,
                                    SellPrice = 0,
                                    RentPrice = 0,
                                    WillTrade = y.WillTrade,
                                    Condition = y.Condition
                                }).ToList();
            }
            return games;
        }
        public List<Models.GameDetailVM> GetGame(int Id, string UserId)
        {
            List<GameDetailVM> gamedetail = new List<GameDetailVM>();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Game game = db.Games.Find(Id);
                gamedetail = db.Games.Include("AspNetUsers").Where(x => x.ApiKey == game.ApiKey && x.IsRented == false && x.IsSold == false).Select(y => new GameDetailVM()
                {
                    Id = y.Id,
                    ApiKey = y.ApiKey,
                    UserId = y.ApplicationUserId,
                    UserName = y.ApplicationUser.UserName,
                    Title = y.Title,
                    Genre = y.Genre,
                    Platform = y.Platform,
                    SellPrice = y.SellPrice,
                    RentPrice = y.RentPrice,
                    WillTrade = y.WillTrade,
                    Condition = y.Condition
                }).ToList();
                gamedetail = gamedetail.Where(x => x.UserId != UserId).ToList();
            }
            return gamedetail;
        }
        public Game UpdateGame(Game game)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Game oldGame = db.Games.Find(game.Id);
                oldGame.SellPrice = game.SellPrice;
                oldGame.RentPrice = game.RentPrice;
                oldGame.WillTrade = game.WillTrade;
                oldGame.IsRented = game.IsRented;
                oldGame.Condition = game.Condition;
                oldGame.IsSold = game.IsSold;
                db.SaveChanges();
            }
            return game;
        }
        public bool DeleteGame(int Id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Game game = db.Games.Find(Id);
                if (game.IsRented == false && game.IsSold == false)
                {
                    db.Games.Remove(game);
                    db.SaveChanges();
                }
                else
                {
                    return false;
                }
                return true;
            }
        }
        public List<Models.GameDetailVM> GetAllGamesForOwner(string username)
        {
            List<Models.GameDetailVM> games;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                ApplicationUser user = db.Users.Where(x => x.UserName == username).FirstOrDefault();
                games = db.Games.Where(x => x.ApplicationUserId == user.Id).Select(y => new GameDetailVM()
                {
                    Id = y.Id,
                    ApiKey = y.ApiKey,
                    UserId = y.ApplicationUserId,
                    UserName = y.ApplicationUser.UserName,
                    Title = y.Title,
                    Genre = y.Genre,
                    Platform = y.Platform,
                    SellPrice = y.SellPrice,
                    RentPrice = y.RentPrice,
                    WillTrade = y.WillTrade,
                    Condition = y.Condition,
                    RentalVM = db.Rentals.Where(z => z.GameId == y.Id).Select(a => new RentalVM()
                    {
                        Id = a.Id,
                        ApplicationUserId = a.ApplicationUserId,
                        UserName = db.Users.Where(z => z.Id == a.ApplicationUserId).FirstOrDefault().UserName,
                        GameId = a.GameId,
                        SoldPrice = a.SoldPrice,
                        RentedPrice = a.RentedPrice,
                        WasTraded = a.WasTraded,
                        WasReturned = a.WasReturned
                    }).ToList()
                }).ToList();
            }
            return games;
        }
        public Game GetGameForRental(int id)
        {
            Game game;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                game = db.Games.Find(id);
            }
            return game;
        }
        public RentalVM AddRental(int gameid, string userid, string action)
        {
            RentalVM rentalvm = new RentalVM();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Rental rental = new Rental();
                rental = db.Rentals.Add(rental);
                Game game = db.Games.Find(gameid);
                rental.GameId = game.Id;
                rental.ApplicationUserId = userid;
                if (action == "rent")
                {
                    rental.RentedPrice = game.RentPrice;
                    rental.SoldPrice = 0;
                    rental.WasTraded = false;
                    rental.WasReturned = false;
                    game.IsRented = true;
                }
                else if (action == "buy")
                {
                    rental.RentedPrice = 0;
                    rental.SoldPrice = game.SellPrice;
                    game.IsSold = true;
                    rental.WasTraded = true;
                    rental.WasReturned = true;
                }
                else if (action == "trade")
                {
                    rental.SoldPrice = 0;
                    rental.RentedPrice = 0;
                    game.IsSold = true;
                    rental.WasTraded = true;
                    rental.WasReturned = true;
                }
                db.SaveChanges();
            }
            return rentalvm;
        }
        public List<RentalVM> GetAllRentalsForRenter(string username)
        {
            List<RentalVM> rentalvm = new List<RentalVM>();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                ApplicationUser user = db.Users.Where(x => x.UserName == username).FirstOrDefault();
                rentalvm = db.Rentals.Where(x => x.ApplicationUserId == user.Id).Select(y => new RentalVM()
                {
                    Id = y.Id,
                    ApplicationUserId = y.ApplicationUserId,
                    UserName = db.Users.Where(z => z.Id == y.ApplicationUserId).FirstOrDefault().UserName,
                    GameId = y.GameId,
                    SoldPrice = y.SoldPrice,
                    RentedPrice = y.RentedPrice,
                    WasTraded = y.WasTraded,
                    WasReturned = y.WasReturned,
                    GameDetailVM = db.Games.Where(z => z.Id == y.GameId).Select(b => new GameDetailVM()
                    {
                        Id = b.Id,
                        ApiKey = b.ApiKey,
                        UserId = b.ApplicationUserId,
                        UserName = b.ApplicationUser.UserName,
                        Title = b.Title,
                        Genre = b.Genre,
                        Platform = b.Platform,
                        SellPrice = 0,
                        RentPrice = 0,
                        WillTrade = b.WillTrade,
                        Condition = b.Condition
                    }).FirstOrDefault()
                }).ToList();
            }
            return rentalvm;
        }
        public Rental GetRental(int id)
        {
            Rental rental;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                rental = db.Rentals.Find(id);
            }
            return rental;
        }

        public Rental UpdateRental(Rental rental)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Rental oldRental = db.Rentals.Find(rental.Id);
                oldRental.WasReturned = rental.WasReturned;
                if (oldRental.WasReturned)
                {
                    Game game = db.Games.Find(oldRental.GameId);
                    game.IsRented = false;
                }
                db.SaveChanges();
            }
            return rental;
        }

        public void DeleteRental(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Rental rental = db.Rentals.Find(id);
                db.Rentals.Remove(rental);
                db.SaveChanges();
            }
        }


        //public int getUserCount(int id1, int id2)
        //{
        //    using (ApplicationDbContext db = new ApplicationDbContext())
        //    {
        //        throw new Exception("error");
        //        // db.Games.Where(x => x.ApiKey == id1).Count();
        //    }
        //}
    }
}